Ext.define('SKJF.controller.BEAIMPORT',{
	extend: 'Ext.app.Controller',
	views: ['PEMBELIAN.v_beaimport','PEMBELIAN.v_beaimport_form'],
	models: ['m_beaimport'],
	stores: ['s_beaimport'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listbeaimport',
		selector: 'Listbeaimport'
	}, {
		ref: 'v_beaimport_form',
		selector: 'v_beaimport_form'
	}, {
		ref: 'Savebeaimport',
		selector: 'v_beaimport_form #save'
	}, {
		ref: 'Createbeaimport',
		selector: 'v_beaimport_form #create'
	}, {
		ref: 'BEAIMPORT',
		selector: 'BEAIMPORT'
	}],


	init: function(){
		this.control({
			'BEAIMPORT': {
				'afterrender': this.beaimportAfterRender
			},
			'v_beaimport_form': {
				'afterlayout': this.beaimportAfterLayout
			},
			'Listbeaimport': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListbeaimport
			},
			'Listbeaimport button[action=create]': {
				click: this.createRecord
			},
			'Listbeaimport button[action=delete]': {
				click: this.deleteRecord
			},
			'Listbeaimport button[action=print]': {
				click: this.printRecords
			},
			'v_beaimport_form button[action=save]': {
				click: this.saveV_beaimport_form
			},
			'v_beaimport_form button[action=create]': {
				click: this.saveV_beaimport_form
			},
			'v_beaimport_form button[action=cancel]': {
				click: this.cancelV_beaimport_form
			}
		});
	},
	
	beaimportAfterRender: function(){
		var beaimportStore = this.getListbeaimport().getStore();
		beaimportStore.load();
	},
	
	beaimportAfterLayout: function(){this.getV_beaimport_form().down('#NOMOR_LPB_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListbeaimport	= this.getListbeaimport();
		var getV_beaimport_form= this.getV_beaimport_form(),
			form			= getV_beaimport_form.getForm();
		var getSavebeaimport	= this.getSavebeaimport();
		var getCreatebeaimport	= this.getCreatebeaimport();
		
		/* grid-panel */
		getListbeaimport.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_beaimport_form.down('#NOMOR_LPB_field').setReadOnly(false);
		getSavebeaimport.setDisabled(true);
		getCreatebeaimport.setDisabled(false);
		getV_beaimport_form.setDisabled(false);
		
		this.getBEAIMPORT().setActiveTab(getV_beaimport_form);		
	},
	
	enableDelete: function(dataview, selections){
		this.getListbeaimport().down('#btndelete').setDisabled(!selections.length);
	},
	
	updateListbeaimport: function(me, record, item, index, e){
		var getBEAIMPORT		= this.getBEAIMPORT();
		var getListbeaimport	= this.getListbeaimport();
		var getV_beaimport_form= this.getV_beaimport_form(),
			form			= getV_beaimport_form.getForm();
		var getSavebeaimport	= this.getSavebeaimport();
		var getCreatebeaimport	= this.getCreatebeaimport();
		
		getSavebeaimport.setDisabled(false);
		getCreatebeaimport.setDisabled(true);
		getV_beaimport_form.down('#NOMOR_LPB_field').setReadOnly(true);		
		getV_beaimport_form.loadRecord(record);
		
		getListbeaimport.setDisabled(true);
		getV_beaimport_form.setDisabled(false);
		getBEAIMPORT.setActiveTab(getV_beaimport_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListbeaimport().getStore();
		var selection = this.getListbeaimport().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "NOMOR_LPB" = "'+selection.data.NOMOR_LPB+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListbeaimport().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_beaimport/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/beaimport.html','beaimport_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_beaimport_form: function(){
		var getBEAIMPORT		= this.getBEAIMPORT();
		var getListbeaimport 	= this.getListbeaimport();
		var getV_beaimport_form= this.getV_beaimport_form(),
			form			= getV_beaimport_form.getForm(),
			values			= getV_beaimport_form.getValues();
		var store 			= this.getStore('s_beaimport');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_beaimport/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('NOMOR_LPB') === values.NOMOR_LPB) {
										return true;
									}
									return false;
								}
							);
							/* getListbeaimport.getView().select(recordIndex); */
							getListbeaimport.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_beaimport_form.setDisabled(true);
					getListbeaimport.setDisabled(false);
					getBEAIMPORT.setActiveTab(getListbeaimport);
				}
			});
		}
	},
	
	createV_beaimport_form: function(){
		var getBEAIMPORT		= this.getBEAIMPORT();
		var getListbeaimport 	= this.getListbeaimport();
		var getV_beaimport_form= this.getV_beaimport_form(),
			form			= getV_beaimport_form.getForm(),
			values			= getV_beaimport_form.getValues();
		var store 			= this.getStore('s_beaimport');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_beaimport/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_beaimport_form.setDisabled(true);
					getListbeaimport.setDisabled(false);
					getBEAIMPORT.setActiveTab(getListbeaimport);
				}
			});
		}
	},
	
	cancelV_beaimport_form: function(){
		var getBEAIMPORT		= this.getBEAIMPORT();
		var getListbeaimport	= this.getListbeaimport();
		var getV_beaimport_form= this.getV_beaimport_form(),
			form			= getV_beaimport_form.getForm();
			
		form.reset();
		getV_beaimport_form.setDisabled(true);
		getListbeaimport.setDisabled(false);
		getBEAIMPORT.setActiveTab(getListbeaimport);
	}
	
});